package com.zunipe.ftpe;

import com.zunipe.ftpe.util.Utils;
import com.zunipe.ftpe.util.ZI18n;

import java.io.IOException;

public class CommandHandler {
    private final Main mMain;
    private String[] mSplitCommand;
    private int mIndex;
    private final FTPConfig mFtpConfig;

    public CommandHandler(Main main) throws IOException {
        mMain = main;
        mFtpConfig = FTPConfig.getInstance();
    }

    public void exec(String cmdLine) throws IOException {
        mSplitCommand = cmdLine.split(" ");
        if (mSplitCommand.length > 0) {
            String command = mSplitCommand[0].toLowerCase();
            mIndex = 1;
            switch (command) {
                case "help":
                    System.out.println(Constants.HELP_STRING);
                    break;
                case "config":
                    System.out.println(mFtpConfig.printConfig());
                    break;
                case "reload":
                    mMain.reload();
                    break;
                case "scan-now":
                    mMain.scanNow();
                    break;
                case "remove":
                    String removeType = nextArg();
                    String removeValue = nextArg();
                    if (removeType != null && removeValue != null) {
                        switch (removeType) {
                            case "timepoint":
                                mFtpConfig.removeTimepoint(removeValue);
                                System.out.println(ZI18n.getString("command.info.delete_successful"));
                                break;
                            case "path":
                                mFtpConfig.removePath(removeValue);
                                System.out.println(ZI18n.getString("command.info.delete_successful"));
                                break;
                            default:
                                System.out.printf(ZI18n.getString("command.error.invalid_type"), removeType);
                        }
                    } else {
                        System.out.println(ZI18n.getString("command.info.remove_uses_way"));
                    }
                    break;
                case "add":
                    String addType = nextArg();
                    String addValue = nextArg();
                    if (addType != null && addValue != null) {
                        switch (addType) {
                            case "timepoint":
                                if (Utils.checkTimepoint(addValue)) {
                                    mFtpConfig.addTimepoint(addValue);
                                    System.out.println(ZI18n.getString("command.info.add_successful"));
                                } else {
                                    System.out.println(ZI18n.getString("command.error.invalid_timepoint"));
                                }
                                break;
                            case "path":
                                mFtpConfig.addPath(addValue);
                                System.out.println(ZI18n.getString("command.info.add_successful"));
                                break;
                            default:
                                System.out.printf(ZI18n.getString("command.error.invalid_type"), addType);
                        }
                    } else {
                        System.out.println(ZI18n.getString("command.info.add_uses_way"));
                    }
                    break;
                default:
                    System.out.printf(ZI18n.getString("command.info.unknown_command"), command);
                    break;
            }
        }
    }

    private String nextArg() {
        if (mIndex < mSplitCommand.length) {
            return mSplitCommand[mIndex++].toLowerCase();
        }
        return null;
    }
}
